'
'   XC Engine (project Minotaur)
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Function ApplicationPath()
	Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
	
	strApplicationPath = objFso.GetParentFolderName(objFso.GetParentFolderName(WScript.ScriptFullName))
	
	If Right(strApplicationPath, 1) <> "\" Then strApplicationPath = strApplicationPath & "\"
	
	ApplicationPath = strApplicationPath
End Function

Function GenerateSessionUniqueNumber()
	GenerateSessionUniqueNumber = "xx" & Year(Date()) & Right("0" & Month(Date()), 2) & Right("0" & Day(Date()), 2) & "." & Right("0" & Hour(Time()), 2) & "." & Right("0" & Minute(Time()), 2) & "." & Right("0" & Second(Time()), 2)
End Function

Class RegularExpression
	Private blnPatternSpecified
	Private objRegExp
	
	Private Sub Class_Initialize()
		blnPatternSpecified = False
		
		Set objRegExp = CreateObject("VBScript.RegExp")
		
		objRegExp.IgnoreCase = True
		objRegExp.Global = False
	End Sub

	Private Sub Class_Terminate()
		Set objRegExp = Nothing
	End Sub
	
	Public Property Get GlobalSearch
		GlobalSearch = objRegExp.Global
	End Property
	
	Public Property Let GlobalSearch(ByVal blnGlobal)
		objRegExp.Global = blnGlobal
	End Property
	
	Public Property Get CaseSensitive
		CaseSensitive = Not(objRegExp.IgnoreCase)
	End Property
	
	Public Property Let CaseSensitive(ByVal blnCaseSensitive)
		objRegExp.IgnoreCase = Not(blnCaseSensitive)
	End Property
	
	Public Property Get MultiLine
		MultiLine = objRegExp.MultiLine
	End Property
	
	Public Property Let MultiLine(ByVal blnMultiLine)
		objRegExp.MultiLine = blnMultiLine
	End Property
	
	Public Default Function SpecifyPattern(ByVal strPattern)
		objRegExp.Pattern = strPattern
		
		blnPatternSpecified = True
		
		Set SpecifyPattern = Me
	End Function
	
	Public Function Test(ByVal strString)
		Test = False
	
		If Not blnPatternSpecified Then Err.Raise 1000
		
		Test = objRegExp.Test(strString)
		
		blnPatternSpecified = False
	End Function
	
	Public Function Match(ByVal strString, ByRef arrMatches)
		Match = False
		
		' Catch this error
		If Not blnPatternSpecified Then Err.Raise 1000
	
		arrMatches = Empty
		strMatches = ""

		If Not objRegExp.Test(strString) Then 
			blnPatternSpecified = False
			
			Exit Function
		End If
		
		Set objMatches = objRegExp.Execute(strString)
		
		If objMatches.Count > 0 Then
			For Each objMatch In objMatches
				If objMatch.SubMatches.Count > 0 Then
					For Each objSubMatch In objMatch.SubMatches
						strMatches = strMatches & objSubMatch & vbCr
					Next
				Else
					strMatches = strMatches & objMatch & vbCr
				End If
			Next
			
			strMatches = Left(strMatches, Len(strMatches)-1)
			arrMatches = Split(strMatches, vbCr)
			
			Match = True
		End If
		
		blnPatternSpecified = False
	End Function
	
	Public Function Replace(ByRef strString, ByVal strReplaceString)
		Replace = False
		
		If Not blnPatternSpecified Then Err.Raise 1000
		
		If objRegExp.Test(strString) Then
			strString = objRegExp.Replace(strString, strReplaceString)

			Replace = True
		End If
		
		blnPatternSpecified = False
	End Function
End Class

Class File
	Private strContent
	Private objFile
	Private objFso
	
	Private Sub Class_Initialize()
		Set objFso = CreateObject("Scripting.FileSystemObject")
	End Sub
	
	Private Sub Class_Terminate()
	End Sub
	
	Public Property Get Content
		Content = strContent
	End Property
	
	Public Function Exists()
	End Function
	
	Public Sub Open(ByVal strFileName)
		Set objFile = objFso.OpenTextFile(strFileName, 8)
	End Sub
	
	Public Sub Close()
	End Sub
End Class

Class Sys
	Public Function GetPathToFileByFileName(ByVal strInitialPath, ByVal strFileName)
		Dim i
		Dim objFso
		ReDim arrSubFolderList(-1)
		
		Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
		
		If Not objFso.FolderExists(strInitialPath) Then
			Err.Raise 5000, "Sys->GetPathToFileByFileName", "Error Description"
		End If
		
		GenerateListOfSubFolders strInitialPath, arrSubFolderList
		
		GetPathToFileByFileName = ""
		
		For i=0 To UBound(arrSubFolderList)
			strPathToFileName = arrSubFolderList(i) & "\" & strFileName
		
			If objFso.FileExists(strPathToFileName) Then
				GetPathToFileByFileName = strPathToFileName
				
				Exit Function
			End If
		Next
	End Function
	
	Private Sub GenerateListOfSubFolders(ByVal strPath, ByRef arrSubFolderList)
		Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
		Set objFolder = objFso.GetFolder(strPath)
		
		ReDim Preserve arrSubFolderList(UBound(arrSubFolderList)+1)
		
		arrSubFolderList(UBound(arrSubFolderList)) = objFolder.Path
	
		For Each objSubFolder in objFolder.SubFolders
			GenerateListOfSubFolders objSubFolder.Path, arrSubFolderList
		Next
	End Sub
End Class

Class HashList
End Class

' Rewrite this class with using Dictionary object
Class ArrayList
	Private intCount
	Private arrArrayList
	
	Private Sub Class_Initialize()
		ReDim arrArrayList(-1)
		
		intCount = 0
	End Sub
	
	Private Sub Class_Terminate()
		For i=0 To intCount-1
			If IsObject(arrArrayList(i)) Then
				Set arrArrayList(i) = Nothing
			End If
		Next		
		
		ReDim arrArrayList(-1)
		
		intCount = 0
	End Sub
	
	Private Sub EvaluateVariable(ByVal varSourceValue, ByRef varDestinationValue)
		If IsObject(varSourceValue) Then
			Set varDestinationValue = varSourceValue
		Else
			varDestinationValue = varSourceValue
		End If
	End Sub
	
	Public Sub Add(ByVal varValue)
		ReDim Preserve arrArrayList(intCount)

		Call EvaluateVariable(varValue, arrArrayList(intCount))
		
		intCount = intCount+1
	End Sub
	
	Public Sub Delete(ByVal varValue)
		ReDim arrTempArrayList(-1)
	
		If VarType(varValue) = 8 Then
			For i=0 To intCount-1
				
			Next
		Else
		End If
		
		intCount = intCount-1
	End Sub
	
	'Public Function Exists(ByVal varValue)
	'	For i=0 To intCount-1
	'		If 
	'	Next
	'End Sub
	
	Public Property Get Count
		Count = intCount
	End Property
	
	Public Default Function Item(ByVal intIndex)
		If intIndex > intCount Or intIndex < 1 Then
			Err.Raise 10000, "ArrayList", "The specified item index is out of bound"
			
			Exit Function
		End If
		
		intIndex = intIndex-1
		
		Call EvaluateVariable(arrArrayList(intIndex), Item)
	End Function
End Class